package com.jwsoft.manager.core.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jwsoft.manager.core.dao.model.EduAddressTree;
import com.jwsoft.manager.core.dao.model.EduAddressTreeInitial;
import com.jwsoft.manager.core.dao.mapper.EduAddressTreeInitialMapper;
import com.jwsoft.manager.core.dao.service.EduAddressTreeInitialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始地址树表（由edu_address解析生成）服务实现类
 *
 * @author zhiyuan
 * @since 2024-01-09
 */
@Service
public class EduAddressTreeInitialServiceImpl extends ServiceImpl<EduAddressTreeInitialMapper, EduAddressTreeInitial> implements EduAddressTreeInitialService {

    @Override
    public List<EduAddressTreeInitial> getLinkByTreeId(Long treeId) {
        ArrayList<EduAddressTreeInitial> treeLink = new ArrayList<>();
        EduAddressTreeInitial eduAddressTree = baseMapper.selectById(treeId);
        if (eduAddressTree == null) {
            return treeLink;
        }
        ArrayList<String> areaTypeList = new ArrayList<>();
        areaTypeList.add("country");
        areaTypeList.add("province");
        areaTypeList.add("city");
        areaTypeList.add("district");
        while (!areaTypeList.contains(eduAddressTree.getAreaType())) {
            treeLink.add(eduAddressTree);
            QueryWrapper<EduAddressTreeInitial> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(EduAddressTreeInitial::getAddressId, eduAddressTree.getParentId());
            eduAddressTree = baseMapper.selectOne(queryWrapper);
        }
        return treeLink;
    }
}
